﻿using System.Linq;
using System.Management.Automation;
using System.Reflection;
using Microsoft.Build.Framework;

namespace DotNet.DevOps.Tasks.Util
{
    internal static class TaskExtensions
    {
        internal static ITask CopyPropertyValuesFrom(this ITask @this, Cmdlet cmdlet)
        {
            var taskProps = @this.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
            var cmdletProps = cmdlet.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);

            taskProps.Where(cprop => cmdletProps.Where(tprop => tprop.Name == cprop.Name).Count() > 0)
                .ToList()
                .ForEach(cprop => cprop.SetValue(@this, cmdletProps.Where(tprop => tprop.Name == cprop.Name).First().GetValue(cmdlet, null), null));

            return @this;
        }
    }
}
